package org.fast.easy.auth.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.fast.easy.auth.domain.vo.LoginVo;
import org.fast.easy.auth.service.IAuthStrategy;
import org.fast.easy.auth.service.ISysLoginService;
import org.fast.easy.common.json.utils.JsonUtils;
import org.fast.easy.core.domain.auth.form.XcxLoginBody;
import org.fast.easy.core.domain.auth.model.XcxLoginUser;
import org.fast.easy.core.util.ValidatorUtils;
import org.fast.easy.framework.satoken.utils.LoginHelper;
import org.fast.easy.system.client.domain.vo.SysClientVo;
import org.springframework.stereotype.Service;

/**
 * 邮件认证策略
 *
 * @author Michelle.Chung
 */
@Slf4j
@Service("xcx" + IAuthStrategy.BASE_NAME)
@RequiredArgsConstructor
public class XcxAuthStrategy extends AbstractAuthStrategy implements IAuthStrategy {

    private final ISysLoginService sysLoginService;


    @Override
    public LoginVo login(String body, SysClientVo client) {
        XcxLoginBody loginBody = JsonUtils.parseObject(body, XcxLoginBody.class);
        ValidatorUtils.validate(loginBody);
        // xcxCode 为 小程序调用 wx.login 授权后获取
        String xcxCode = loginBody.getXcxCode();
        // 多个小程序识别使用
        String appid = loginBody.getAppid();

        // todo 以下自行实现
        // 校验 appid + appsrcret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
        String openid = "";
        XcxLoginUser loginUser = sysLoginService.getUserInfoByOpenid(openid);
        return getUserLoginVo(loginUser, client, openid);
    }

}
